package cn.anc.aonicardv.module.adpter.recorder;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.anc.aonicardv.BuildConfig;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.module.adpter.listener.OnCommonRecycleItemClickListener;
import cn.anc.aonicardv.module.ui.recorder.RecorderSettingActivity;
import cn.anc.aonicardv.util.ui.CarSettingGuiHandler;
import cn.anc.aonicardv.widget.CustomDialog;
import cn.anc.httpcontrolutil.CarControl;
import cn.anc.httpcontrolutil.CarControlSettings;

/**
 * Created by yangdai on 2017/6/20.
 */

public class RecorderSettingAdapter extends RecyclerView.Adapter {

    private List<CarControlSettings.SettingItem> settingItemList;
    private RecorderSettingActivity recorderSettingActivity;
    private boolean isAvailabilityUpdate;

    public RecorderSettingAdapter(RecorderSettingActivity recorderSettingActivity) {
        this.recorderSettingActivity = recorderSettingActivity;
    }

    public void setData(List<CarControlSettings.SettingItem> settingItemList) {
        List<CarControlSettings.SettingItem> removeItem = new ArrayList<>();
        if (null!=settingItemList&&settingItemList.size() > 0) {
            for (int i=0; i<settingItemList.size(); i++) {
                if (settingItemList.get(i).settingItemKey.equals(CarSettingGuiHandler.SETTING_ITEM_MIC)||settingItemList.get(i).settingItemKey.equals(CarSettingGuiHandler.SETTING_ITEM_TIME)) {
                    settingItemList.remove(i);   //执行录音开关选项
                }
            }
            removeItem.clear();
        }
        this.settingItemList = settingItemList;
        notifyDataSetChanged();
    }

    public void setAvailabilityUpdate(boolean availabilityUpdate) {
        isAvailabilityUpdate = availabilityUpdate;
        notifyDataSetChanged();
    }

    public List<CarControlSettings.SettingItem> getSettingItemList() {
        return settingItemList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recorder_setting, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        final CarControlSettings.SettingItem settingItem = settingItemList.get(position);
        if (settingItem == null)
            return;
        if (position<settingItemList.size()&&position==settingItemList.size()-1){
            viewHolder.vItemLine.setVisibility(View.GONE);
        }else{
            viewHolder.vItemLine.setVisibility(View.VISIBLE);
        }
        CarControlSettings.SettingItemGuiResource itemGuiResource = CarControlSettings.getSettingItemGuiResource(settingItem.settingItemKey);
        if (itemGuiResource != null) {
            viewHolder.settingNameTv.setText(itemGuiResource.settingItemDisplayStrId);
        }
        viewHolder.iconTv.setVisibility(View.GONE);


        if (null != settingItemList.get(position).settingItemOptions && settingItemList.get(position).settingItemOptions.size() == 2 && (CarSettingGuiHandler.SETTING_ITEM_OPTION_ON.equals(settingItem.settingItemValue) || CarSettingGuiHandler.SETTING_ITEM_OPTION_OFF.equals(settingItem.settingItemValue))) {
            viewHolder.layout.setVisibility(View.GONE);
            viewHolder.switchButton.setVisibility(View.VISIBLE);
            final boolean[] isCheck = {false};
            if (CarSettingGuiHandler.SETTING_ITEM_OPTION_ON.equals(settingItem.settingItemValue)) {
                isCheck[0] = true;
                viewHolder.switchButton.setImageDrawable(recorderSettingActivity.getResources().getDrawable(R.mipmap.switch_btn_open));
            } else {
                isCheck[0] = false;
                viewHolder.switchButton.setImageDrawable(recorderSettingActivity.getResources().getDrawable(R.mipmap.switch_btn_close));
            }

            viewHolder.switchButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        viewHolder.switchButton.setImageDrawable(isCheck[0] ? recorderSettingActivity.getResources().getDrawable(R.mipmap.switch_btn_close) : recorderSettingActivity.getResources().getDrawable(R.mipmap.switch_btn_open));
                        isCheck[0] = !isCheck[0];
                        String option = isCheck[0] ? CarSettingGuiHandler.SETTING_ITEM_OPTION_ON : CarSettingGuiHandler.SETTING_ITEM_OPTION_OFF;
                        if (!option.equals(settingItem.settingItemValue)) {
                            settingItem.settingItemValue = option;
                            recorderSettingActivity.progressDialog.show();
                            CarControl.doSetSettingItemValue(settingItem.settingItemKey, option, new CarControl.OnCarControlCallback() {
                                @Override
                                public void OnCommandCallback(int code, CarControl.CommandResponseInfo info) {
                                    recorderSettingActivity.progressDialog.dismiss();
                                }
                            });
                        }
                }
            });


        } else {
            viewHolder.layout.setVisibility(View.VISIBLE);
            viewHolder.switchButton.setVisibility(View.GONE);
            viewHolder.moreTv.setVisibility(View.VISIBLE);
            viewHolder.valueTv.setText(settingItem.settingItemValue);


            if (settingItem.settingItemKey.equals(CarSettingGuiHandler.SETTING_ITEM_STAMP)) {
                viewHolder.valueTv.setVisibility(View.GONE);
            } else {
                viewHolder.valueTv.setVisibility(View.VISIBLE);
            }
            if (!settingItem.settingItemKey.equals(CarSettingGuiHandler.SETTING_ITEM_STAMP)) {
                if (BuildConfig.FLAVOR.equals("blackbox") && settingItem.settingItemKey.equals(CarSettingGuiHandler.SETTING_ITEM_SN)) {
                    viewHolder.valueTv.setText("38661");
                    viewHolder.moreTv.setVisibility(View.GONE);
                } else if (!settingItem.settingItemKey.equals(CarSettingGuiHandler.SETTING_ITEM_EV)
                        && itemGuiResource.settingItemGroupId != CarSettingGuiHandler.CAR_SETTING_GROUP_ID.GROUP_RECORDER_MSG
                ) {
                    CarControlSettings.SettingOptionGuiResource optionGuiResource = CarControlSettings.getSettingOptionGuiResourceByKey(settingItem.settingItemKey, settingItem.settingItemValue);
                    if (optionGuiResource != null) {
                        viewHolder.valueTv.setText(optionGuiResource.settingOptionDisplayStrId);
                    }
                } else {
                    viewHolder.valueTv.setText(settingItem.settingItemValue);
                    if (itemGuiResource.settingItemGroupId == CarSettingGuiHandler.CAR_SETTING_GROUP_ID.GROUP_RECORDER_MSG){
                        viewHolder.moreTv.setVisibility(View.GONE);
                    }
                    if (settingItem.settingItemKey.equals(CarSettingGuiHandler.SETTING_ITEM_FORMAT_SD_CARD)) {
                        viewHolder.moreTv.setVisibility(View.VISIBLE);
                    }
                    if (settingItem.settingItemKey.equals(CarSettingGuiHandler.SETTING_ITEM_FIRMWARE_UPDATE) && isAvailabilityUpdate) {
                        viewHolder.iconTv.setVisibility(View.VISIBLE);
                    }

                }
            }
        }

        viewHolder.rootLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null)
                    onItemClickListener.OnItemClick(position);
            }
        });
    }
    private CustomDialog mParkingModeDialog;
    @Override
    public int getItemCount() {
        return settingItemList == null ? 0 : settingItemList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R2.id.tv_setting_name)
        TextView settingNameTv;
        @BindView(R2.id.tv_value)
        TextView valueTv;
        @BindView(R2.id.tv_more)
        TextView moreTv;
        @BindView(R2.id.tv_icon)
        TextView iconTv;
        @BindView(R2.id.layout)
        LinearLayout layout;
        @BindView(R2.id.switch_button)
        ImageView switchButton;
        @BindView(R2.id.layout_root)
        LinearLayout rootLayout;
        @BindView(R2.id.v_item_line)
        View vItemLine;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public OnCommonRecycleItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnCommonRecycleItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
